package com.lm.paymentadmin.service;

import com.lm.paymentadmin.common.Result;
import com.lm.paymentadmin.common.config.OSSConfig;
import com.lm.paymentadmin.util.OSSBootUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


@Service
public class CommonService {

    @Resource
    private OSSConfig ossConfig;

    /**
     * 上传文件至阿里云 oss
     *
     * @param file
     * @param appSource
     * @return Result
     */
    public Result uploadOSS(MultipartFile file,String appSource) throws IOException {
        // 格式化时间
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        // 高依赖版本 oss 上传工具
        String ossFileUrlBoot = OSSBootUtil.upload(ossConfig, file, appSource+ "/" +format);
        return Result.ok("上传成功!",ossFileUrlBoot);
    }

    public Result delete(String objectName) {
        return OSSBootUtil.delete(objectName, ossConfig);
    }
}
